﻿using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;
using ZhuXingFamilys.ExportExcel.entity;

namespace ZhuXingFamilys.ExportExcel.utils
{
    public class StoreElementReadInfo
    {
        // 根据清单表名称获取清单表
        public static IList<string> GetEntity(Document doc, string dataStorageName ,string guid, string key)
        {
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            var dataStorageList = collector.OfClass(typeof(DataStorage)).ToList();
            var dataStorage = dataStorageList.FirstOrDefault(o => o.Name == dataStorageName);
            if (dataStorage != null)
            {
                //获取Schema
                var schemaGuids = dataStorage.GetEntitySchemaGuids();
                Schema targetSchema = null;
                foreach (var schemaGuid in schemaGuids)
                {
                    Schema schema = Schema.Lookup(schemaGuid);
                    if (schema.GUID == new Guid(guid))
                    {
                        targetSchema = schema;
                        break;
                    }
                }
                if (targetSchema == null)
                {
                    MessageBox.Show("targetSchema is not exist");
                }
                Entity targetEntity = dataStorage.GetEntity(targetSchema);
                if (targetEntity == null)
                {
                    MessageBox.Show("targetEntity is not exist");
                }
                IList<string> byteArray = targetEntity.Get<IList<string>>(key);
                return byteArray;
            }
            return null;
        }
        // 获取已保存的清单表名称和GUID
        public static IList<SchemaName> GetSchemaList(Document doc ,string dataStorageName)
        {
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            var dataStorageList = collector.OfClass(typeof(DataStorage)).ToList();
            var dataStorage = dataStorageList.FirstOrDefault(o => o.Name == dataStorageName);
            if (dataStorage != null)
            {
                //获取Schema
                var schemaGuids = dataStorage.GetEntitySchemaGuids();
                List<SchemaName> schemaNameList = new List<SchemaName>();
                foreach (var schemaGuid in schemaGuids)
                {
                    Schema schema = Schema.Lookup(schemaGuid);
                    SchemaName schemaName = new SchemaName();
                    schemaName.Name = schema.SchemaName;
                    schemaName.Guid = schema.GUID.ToString();
                    schemaNameList.Add(schemaName);
                }
                return schemaNameList;
            }
            return null;
        }
    }
}
